package smedi.ismedi.web.bff.controller.equipment;

import com.smedi.ismedi.equipment.api.dto.request.*;
import com.smedi.ismedi.equipment.api.dto.response.EquipmentResponse;
import com.smedi.ismedi.equipment.api.feign.RemoteEquipmentFeignApi;
import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import smedi.ismedi.web.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 特种设备
 *
 * @author xuyuanqiao
 * @date 2021/09/15
 */
@Slf4j
@Api(value = "特种设备管理接口", tags = {"特种设备管理接口"})
@RequestMapping(value = "/webbff/v1")
@RestController
@RequiredArgsConstructor
public class EquipmentController {
    private final RemoteEquipmentFeignApi remoteEquipmentFeignApi;

    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;


    /**
     * 获取 机械设备 token
     *
     * @return ResponseData<String>
     */
    @ApiOperation(value = "获取 机械设备 token", nickname = "获取 机械设备 token")
    @PostMapping(value = "/business-token")
    ResponseData<String> getBusinessToken() {
        return remoteEquipmentFeignApi.getBusinessToken();
    }

    /**
     * 新增 机械设备
     *
     * @param equipmentCreateRequest 机械设备 新增 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "机械设备新增", nickname = "机械设备新增")
    @PostMapping(value = "/equipment/create")
    ResponseData<EquipmentResponse> createEquipment(@Validated @RequestBody EquipmentCreateRequest equipmentCreateRequest) {
        return remoteEquipmentFeignApi.createEquipment(equipmentCreateRequest);
    }

    /**
     * 编辑 机械设备
     *
     * @param equipmentEditRequest 机械设备 编辑 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "机械设备编辑", nickname = "机械设备编辑")
    @PostMapping(value = "/equipment/edit")
    ResponseData<EquipmentResponse> editEquipment(@Validated @RequestBody EquipmentEditRequest equipmentEditRequest) {
        return remoteEquipmentFeignApi.editEquipment(equipmentEditRequest);
    }

    /**
     * 删除 机械设备
     *
     * @param equipmentRemoveRequest 机械设备 删除 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "删除 机械设备")
    @PostMapping(value = "/equipment/remove")
    ResponseData<Boolean> removeEquipment(@Validated @RequestBody EquipmentRemoveRequest equipmentRemoveRequest) {
        return remoteEquipmentFeignApi.removeEquipment(equipmentRemoveRequest);
    }

    /**
     * 获取 机械设备 单一
     *
     * @param equipmentOneRequest 机械设备 单一 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "获取 机械设备 单一", nickname = "获取 机械设备 单一")
    @PostMapping(value = "/equipment/one")
    ResponseData<EquipmentResponse> getEquipmentOne(@Validated @RequestBody EquipmentOneRequest equipmentOneRequest) {
        return remoteEquipmentFeignApi.getEquipmentOne(equipmentOneRequest);
    }

    /**
     * 获取 机械设备 列表
     *
     * @param equipmentListRequest 机械设备 列表 请求
     * @return ResponseData<List < EquipmentResponse>>
     */
    @ApiOperation(value = "获取 机械设备 列表", nickname = "获取 机械设备 列表")
    @PostMapping(value = "/equipment/list")
    ResponseData<List<EquipmentResponse>> getEquipmentList(@Validated @RequestBody EquipmentListRequest equipmentListRequest) {
        return remoteEquipmentFeignApi.getEquipmentList(equipmentListRequest);
    }

    /**
     * 获取 机械设备 分页
     *
     * @param equipmentPageRequest 机械设备 分页 请求
     * @return ResponseData<PaginationData < EquipmentResponse>>
     */
    @ApiOperation(value = "获取 机械设备 分页", nickname = "获取 机械设备 分页")
    @PostMapping(value = "/equipment/page")
    ResponseData<PaginationData<EquipmentResponse>> getEquipmentPage(@Validated @RequestBody EquipmentPageRequest equipmentPageRequest) {
        return remoteEquipmentFeignApi.getEquipmentPage(equipmentPageRequest);
    }

    /**
     * 新增 起重机
     *
     * @param craneCreateRequest 起重机 新增 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "新增 起重机", nickname = "新增 起重机")
    @PostMapping(value = "/crane/create")
    ResponseData<EquipmentResponse> createCrane(@Validated @RequestBody CraneCreateRequest craneCreateRequest) {
        return remoteEquipmentFeignApi.createCrane(craneCreateRequest);
    }

    /**
     * 主体院审批 起重机
     *
     * @param craneReviewOrganizationRequest 起重机 主体院审批 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "主体院审批 起重机", nickname = "主体院审批 起重机")
    @PostMapping(value = "/crane/review-organization")
    ResponseData<EquipmentResponse> reviewOrganizationCrane(@Validated @RequestBody CraneReviewOrganizationRequest craneReviewOrganizationRequest) {
        return remoteEquipmentFeignApi.reviewOrganizationCrane(craneReviewOrganizationRequest);
    }

    /**
     * 总院审批 起重机
     *
     * @param craneReviewInstitutionRequest 起重机 总院审批 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "总院审批 起重机", nickname = "总院审批 起重机")
    @PostMapping(value = "/crane/review-institution")
    ResponseData<EquipmentResponse> reviewInstitutionCrane(@Validated @RequestBody CraneReviewInstitutionRequest craneReviewInstitutionRequest) {
        return remoteEquipmentFeignApi.reviewInstitutionCrane(craneReviewInstitutionRequest);
    }

    /**
     * 编辑 起重机
     *
     * @param craneEditRequest 起重机 编辑 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "编辑 起重机", nickname = "编辑 起重机")
    @PostMapping(value = "/crane/edit")
    ResponseData<EquipmentResponse> editCrane(@Validated @RequestBody CraneEditRequest craneEditRequest) {
        return remoteEquipmentFeignApi.editCrane(craneEditRequest);
    }

    /**
     * 撤回审批 起重机
     *
     * @param craneCancelRequest 起重机 撤回审批 请求
     * @return ResponseData<EquipmentResponse>
     */
    @ApiOperation(value = "撤回审批 起重机", nickname = "撤回审批 起重机")
    @PostMapping(value = "/crane/cancel")
    ResponseData<EquipmentResponse> cancelCrane(@Validated @RequestBody CraneCancelRequest craneCancelRequest){
        return remoteEquipmentFeignApi.cancelCrane(craneCancelRequest);
    }

    /**
     * 预览准入证
     *
     * @param accessCardExportRequest 准入证 导出 请求
     * @return ResponseData<String>
     */
    @ApiOperation(httpMethod = "POST", value = "预览准入证")
    @PostMapping(value = "/previewAccessCard")
    ResponseData<String> previewAccessCard(@Validated @RequestBody AccessCardExportRequest accessCardExportRequest, HttpServletResponse response) {
        String signKey = remoteEquipmentFeignApi.exportAccessCard(accessCardExportRequest).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        String url = FileHelper.getUrl(FDFS_SERVICE_ADDRESS_OUT, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(url);
    }

    /**
     * 导出准入证
     *
     * @param accessCardExportRequest 准入证 导出 请求
     * @return ResponseData<String>
     */
    @ApiOperation(httpMethod = "POST", value = "导出准入证")
    @PostMapping(value = "/exportAccessCard")
    ResponseData<Boolean> exportAccessCard(@Validated @RequestBody AccessCardExportRequest accessCardExportRequest, HttpServletResponse response) {
        String signKey = remoteEquipmentFeignApi.exportAccessCard(accessCardExportRequest).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }


    /**
     * 导出特种设备/流动式起重机
     *
     * @param equipmentExportRequest 特种设备/流动式起重机 导出 请求
     * @return ResponseEntity<byte [ ]>
     */
    @ApiOperation(httpMethod = "POST", value = "导出特种设备/流动式起重机")
    @PostMapping(value = "/equipment/export")
    ResponseEntity<byte[]> exportEquipment(@Validated @RequestBody EquipmentExportRequest equipmentExportRequest){
        return remoteEquipmentFeignApi.exportEquipment(equipmentExportRequest);
    };
}
